package Chuong08;
import java.io.*;
import java.net.*;

public class ThreadedEchoServer {  
   public static void main(String[] args )  {  
      try {  
         int i = 1;
         ServerSocket s = new ServerSocket(8010);

         while (true) {  
            Socket incoming = s.accept( );
            System.out.println("Spawning " + i);
            Thread t = new ThreadedEchoHandler(incoming, i);
            t.start();
            i++;
         }
      }
      catch (Exception e)   {  
         e.printStackTrace();
      }
   }
}

/**
   This class handles the client input for one server socket
   connection. 
*/
class ThreadedEchoHandler extends Thread { 
   
   public ThreadedEchoHandler(Socket i, int c)   { 
      incoming = i; counter = c; 
   }

   public void run()   {  
      try {  
         BufferedReader in = new BufferedReader
            (new InputStreamReader(incoming.getInputStream()));
         PrintWriter out = new PrintWriter
            (incoming.getOutputStream(), true /* autoFlush */);

         out.println( "Hello! Enter BYE to exit." );

         boolean done = false;
         while (!done) {  
            String str = in.readLine();
            if (str == null) done = true;
            else
            {  
               out.println("Echo (" + counter + "): " + str);

               if (str.trim().equals("BYE"))
                  done = true;
            }
         }
         incoming.close();
      }
      catch (Exception e)
      {  
         e.printStackTrace();
      }
   }

   private Socket incoming;
   private int counter;
}

